package org.yxing.demo.test;

import java.util.ArrayList;

public class ActionBar {
    
    private ArrayList<ActionBarItem> mItems;
    
    public ActionBar() {
        mItems = new ArrayList<ActionBarItem>();
    }
    
    public void addItem(int id, int order, int iconRes) {
        addInternal(id, order, iconRes);
    }
    
    public void addItem(int id, int iconRes) {
        addInternal(id, 0, iconRes);
    }
    
    private void addInternal(int id, int order, int iconRes) {
        ActionBarItem item = new ActionBarItem(id, order, iconRes);
        if (order == 0) 
            mItems.add(0, item);
        else
            mItems.add(findInsertIndex(order), item);
    }
    
    private int findInsertIndex(int order) {
        for (int i = mItems.size() - 1; i >= 0; i --) {
            ActionBarItem item = mItems.get(i);
            if (item.getOrder() <= order)
                return i + 1;
        }
        
        return 0;
    }
    
    public ActionBarItem getItem(int index) {
        return mItems.get(index);
    }
    
    public int size() {
       return mItems.size();
    }
}
